# store all the sprites and groups

import pygame
from typing import List


from game.container import Variables, CONSTANT
from game.data.assets.assetManager import load_image
from game.data.plot.plot import load_text, Dialog
from game.sprites.base import Frames
from game.sprites.mob.mob import Mob
from game.stat import Stat
from game.world.coordinate import Coordinate
from game.sprites.player.player import Player

background = load_image('test/ground.png').convert()
# Group of visible_sprites
class YSortCameraGroup(pygame.sprite.Group):
    def __init__(self, background:pygame.Surface):
        #general setup
        super().__init__()
        self.display_surface =Variables.screen
        self.half_width=CONSTANT.APP.RESOLUTION[0]//2
        self.half_height=CONSTANT.APP.RESOLUTION[1]//2
        self.offset=pygame.math.Vector2()
        #create the ground
        self.floor_surface=background
        self.floor_rect=self.floor_surface.get_rect(topleft=(0,0))

    def custom_draw(self,player):
        self.offset.x=player.rect.centerx-self.half_width
        self.offset.y=player.rect.centery-self.half_height

        #draw the ground
        floor_offset_pos=self.floor_rect.topleft - self.offset
        self.display_surface.blit(self.floor_surface,floor_offset_pos)
        for sprite in sorted(self.sprites(),key=lambda sprite: sprite.rect.centery):
            offset_pos = sprite.rect.topleft - self.offset
            # sprite.frames.
            self.display_surface.blit(sprite.surface,offset_pos)

class GroupsContainer:
    visible_group = YSortCameraGroup(background)
    obstacle_group = pygame.sprite.Group()

    Variables.visible_group = visible_group
    Variables.obstacle_group = obstacle_group


class SpritesContainer:
    @classmethod
    def getPlayer(cls) -> Player:
        return cls.PLAYER.player
    
    @classmethod
    def getMobs(cls) -> List[Mob]:
        props = dir(cls.MOB)[26:]
        ret = []
        for prop in props:
            attr = getattr(cls.MOB, prop)
            if isinstance(attr, Mob):
                ret.append(attr)
        return ret
    
    class MOB:
        cbs_img = load_image("demo/cbs.jpg", 4)
        cbs = Mob(
            Coordinate((CONSTANT.APP.RESOLUTION[0]//2 + 50,CONSTANT.APP.RESOLUTION[1]//2+ 50)),
            (GroupsContainer.obstacle_group, GroupsContainer.visible_group),
            Frames((cbs_img, cbs_img),(cbs_img, cbs_img),(cbs_img, cbs_img),(cbs_img, cbs_img),(cbs_img, cbs_img),(cbs_img, cbs_img),(cbs_img, cbs_img),(cbs_img, cbs_img),),
            dialog = Dialog(load_text("cbs.txt")),
            can_interact = True,
            can_communicate = True
        )
        
        xrh_img = load_image("demo/xrh.png", 4)
        xrh = Mob(
            Coordinate((1160, 607)),
            (GroupsContainer.obstacle_group, GroupsContainer.visible_group),
            Frames((xrh_img, xrh_img),(xrh_img, xrh_img),(xrh_img, xrh_img),(xrh_img, xrh_img),(xrh_img, xrh_img),(xrh_img, xrh_img),(xrh_img, xrh_img),(xrh_img, xrh_img),),
            dialog = Dialog(load_text("xrh.txt")),
            can_interact = True,
            can_communicate = True
        )
        
        zmj_img = load_image("demo/zmj.png", 4)
        zmj = Mob(
            Coordinate((1130, 804)),
            (GroupsContainer.obstacle_group, GroupsContainer.visible_group),
            Frames((zmj_img, zmj_img),(zmj_img, zmj_img),(zmj_img, zmj_img),(zmj_img, zmj_img),(zmj_img, zmj_img),(zmj_img, zmj_img),(zmj_img, zmj_img),(zmj_img, zmj_img),),
            dialog = Dialog(load_text("zmj.txt")),
            can_interact = True,
            can_communicate = True
        )
        
        wsq_img = load_image("demo/wsq.jpg", 4)
        wsq = Mob(
            Coordinate((820, 808)),
            (GroupsContainer.obstacle_group, GroupsContainer.visible_group),
            Frames((wsq_img, wsq_img),(wsq_img, wsq_img),(wsq_img, wsq_img),(wsq_img, wsq_img),(wsq_img, wsq_img),(wsq_img, wsq_img),(wsq_img, wsq_img),(wsq_img, wsq_img),),
            dialog = Dialog(load_text("wsq.txt")),
            can_interact = True,
            can_communicate = True
        )
        
    class PLAYER:
        PlayerFrames = Frames(
            # (load_image("test/player/up_walk1.png"), load_image("test/player/up_walk2.png"),
            #  load_image("test/player/up_walk3.png"),load_image("test/player/up_walk4.png"), ),
            # (load_image("test/player/up_walk1.png"), load_image("test/player/up_walk2.png"),
            #  load_image("test/player/up_walk3.png"),load_image("test/player/up_walk4.png"),),
            # (load_image("test/player/right_walk1.png"),load_image("test/player/right_walk2.png"),
            #  load_image("test/player/right_walk3.png"),load_image("test/player/right_walk4.png"), ),
            # ( load_image("test/player/down_walk1.png"),load_image("test/player/down_walk2.png"),
            #   load_image("test/player/down_walk3.png"),load_image("test/player/down_walk4.png"),),
            # ( load_image("test/player/down_walk1.png"),load_image("test/player/down_walk2.png"),
            #   load_image("test/player/down_walk3.png"),load_image("test/player/down_walk4.png"),),
            # (load_image("test/player/down_walk1.png"),load_image("test/player/down_walk2.png"),
            #  load_image("test/player/down_walk3.png"),load_image("test/player/down_walk4.png"), ),
            # ( load_image("test/player/left_walk1.png"),load_image("test/player/left_walk2.png"),
            #   load_image("test/player/left_walk3.png"),load_image("test/player/left_walk4.png"),),
            # (load_image("test/player/up_walk1.png"), load_image("test/player/up_walk2.png"),
            #  load_image("test/player/up_walk3.png"),load_image("test/player/up_walk4.png"),)
            (load_image("demo/player/up_0.png"), load_image("demo/player/up_1.png"), load_image("demo/player/up_2.png"),
             load_image("demo/player/up_3.png"),),
            (load_image("demo/player/up_0.png"), load_image("demo/player/up_1.png"), load_image("demo/player/up_2.png"),
             load_image("demo/player/up_3.png"),),
            (load_image("demo/player/right_0.png"), load_image("demo/player/right_1.png"),
             load_image("demo/player/right_2.png"), load_image("demo/player/right_3.png"),),
            (load_image("demo/player/down_0.png"), load_image("demo/player/down_1.png"),
             load_image("demo/player/down_2.png"), load_image("demo/player/down_3.png"),),
            (load_image("demo/player/down_0.png"), load_image("demo/player/down_1.png"),
             load_image("demo/player/down_2.png"), load_image("demo/player/down_3.png"),),
            (load_image("demo/player/down_0.png"), load_image("demo/player/down_1.png"),
             load_image("demo/player/down_2.png"), load_image("demo/player/down_3.png"),),
            (load_image("demo/player/left_0.png"), load_image("demo/player/left_1.png"),
             load_image("demo/player/left_2.png"), load_image("demo/player/left_3.png"),),
            (load_image("demo/player/up_0.png"), load_image("demo/player/up_1.png"), load_image("demo/player/up_2.png"),
             load_image("demo/player/up_3.png"),)
        )

        # just for test 8.19
        player = Player(Coordinate((CONSTANT.APP.RESOLUTION[0]//2-20,CONSTANT.APP.RESOLUTION[1]//2-20)), (GroupsContainer.visible_group, ), PlayerFrames, Stat(health=80))

